package helpers;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.channels.Channels;

import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileReadChannel;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileWriteChannel;
import com.google.appengine.api.files.GSFileOptions.GSFileOptionsBuilder;

public class GoogleStorageHelper {
	public static String BUCKETNAME = "static.hatforrent.com";
	
	/**
	 * Write Content to file xml
	 * @param byte[] content
	 * @param filename
	 * 
	 * (View more mine type on http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm)
	 */
	public static void saveFileXML (byte[] content, String filename) {
		String mineType = "application/xml";
		writeFileToStorage(content, mineType, filename);
	}
	
	/**
	 * Write Content to file text
	 * @param byte[] content
	 * @param filename
	 */
	public static void saveFileText (byte[] content, String filename) {
		String mineType = "text/plain";
		writeFileToStorage(content, mineType, filename);
	}
	
	
	/**
	 * Write content to google storage 
	 * @param content
	 * @param mineType
	 * @param filename
	 */
	public static void writeFileToStorage (byte[] content, String mineType, String filename) {
		try {
			FileService fileService = FileServiceFactory.getFileService();
		    GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
		       .setBucket(BUCKETNAME)
		       .setKey(filename)
		       .setMimeType(mineType)
		       .setAcl("public_read").setCacheControl("public, max-age=6000");
		       
		    
		    AppEngineFile writableFile =
		        fileService.createNewGSFile(optionsBuilder.build());
		    // Open a channel to write to it
		    boolean lock = true;
		    FileWriteChannel writeChannel =
		        fileService.openWriteChannel(writableFile, lock);
		 
		    // This time we write to the channel using standard Java
		    writeChannel.write(java.nio.ByteBuffer.wrap(content));
		    // Now finalize
		    writeChannel.closeFinally();
		    System.out.println("Done writing...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Read file from google storage
	 * @param fileName ("/gs/" + GoogleStorageHelper.BUCKETNAME + "/" + namefile)
	 * @return
	 * @see: "/gs/static.hatforrent.com/hoanbn.txt"
	 */
	public static String readFileGoogleStorage(String fileName) {
		String content = "";
		try {

			FileService fileService = FileServiceFactory.getFileService();
			AppEngineFile readableFile = new AppEngineFile(fileName);
			FileReadChannel readChannel = fileService.openReadChannel(
					readableFile, false);

			InputStream is = Channels.newInputStream(readChannel);
			ByteArrayOutputStream tempBAOS = new ByteArrayOutputStream();

			int length;
			byte[] buffer = new byte[10240];
			while ((length = is.read(buffer, 0,
					buffer.length)) != -1) {
				tempBAOS.write(buffer, 0, length);
			}
			
			content = new String(tempBAOS.toByteArray());
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
		
		return content;
	}
}
